﻿using System;
using System.ComponentModel;
using System.Drawing;

namespace HML
{
    /// <summary>
    /// 全局自定义按钮
    /// </summary>
    [TypeConverter(typeof(PropertyOrderConverter))]
    public class TabControlPlusGlobalCustomButtton
    {
        #region 新增事件

        public delegate void GlobalCustomButttonEventHandler(object sender, TabControlPlusGlobalCustomButttonEventArgs e);
        private event GlobalCustomButttonEventHandler globalCustomButttonClick;
        /// <summary>
        /// 全局自定义按钮单击事件
        /// </summary>
        [Description("全局自定义按钮单击事件")]
        [Category("杂项_全局自定义按钮")]
        public event GlobalCustomButttonEventHandler GlobalCustomButttonClick
        {
            add { this.globalCustomButttonClick += value; }
            remove { this.globalCustomButttonClick -= value; }
        }

        #endregion

        #region 属性

        private string key = "";
        /// <summary>
        /// 按钮Key（必须唯一，可用于索引）
        /// </summary>
        [Description("按钮Key（必须唯一，可用于索引）")]
        [PropertyOrder(-186)]
        [DefaultValue("")]
        public string Key
        {
            get { return this.key; }
            set
            {
                if (this.key == value)
                    return;

                this.key = value;
            }
        }

        private bool visible = true;
        /// <summary>
        /// 按钮是否显示
        /// </summary>
        [Description("按钮是否显示")]
        [PropertyOrder(-184)]
        [DefaultValue(true)]
        public bool Visible
        {
            get { return this.visible; }
            set
            {
                if (this.visible == value)
                    return;

                this.visible = value;
                this.InitializeInvalidate();
            }
        }

        private bool enabled = true;
        /// <summary>
        /// 按钮启用状态
        /// </summary>
        [Description("按钮启用状态")]
        [PropertyOrder(-182)]
        [DefaultValue(true)]
        public bool Enabled
        {
            get { return this.enabled; }
            set
            {
                if (this.enabled == value)
                    return;

                this.enabled = value;
                this.Invalidate();
            }
        }

        private Size size = new Size(14, 14);
        /// <summary>
        /// 按钮Size
        /// </summary>
        [Description("按钮Size")]
        [PropertyOrder(-180)]
        [DefaultValue(typeof(Size), "14, 14")]
        public Size Size
        {
            get { return this.size; }
            set
            {
                if (this.size == value || value.Width < 0 || value.Height < 0)
                    return;

                this.size = value;
                this.InitializeInvalidate();
            }
        }

        private LRMargin lRMargin = new LRMargin(1, 1);
        /// <summary>
        /// 按钮左右外边距
        /// </summary>
        [Description("按钮左右外边距")]
        [PropertyOrder(-179)]
        [DefaultValue(typeof(LRMargin), "1,1")]
        public LRMargin LRMargin
        {
            get { return this.lRMargin; }
            set
            {
                if (this.lRMargin == value || value.Left < 0 || value.Right < 0)
                    return;

                this.lRMargin = value;
                this.InitializeInvalidate();
            }
        }

        private string tipText = "";
        /// <summary>
        /// 按钮提示文本
        /// </summary>
        [Description("按钮提示文本")]
        [PropertyOrder(-178)]
        [DefaultValue("")]
        [Browsable(true)]
        public string TipText
        {
            get { return this.tipText; }
            set
            {
                if (this.tipText == value)
                    return;

                this.tipText = value;
            }
        }

        private Image imageNormal = null;
        /// <summary>
        /// 按钮图片(正常)
        /// </summary>
        [Description("按钮图片(正常)")]
        [PropertyOrder(-176)]
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public Image ImageNormal
        {
            get { return this.imageNormal; }
            set
            {
                if (this.imageNormal == value)
                    return;

                this.imageNormal = value;
                this.Invalidate();
            }
        }

        private Image imageEnter = null;
        /// <summary>
        /// 按钮图片(鼠标进入)
        /// </summary>
        [Description("按钮图片(鼠标进入)")]
        [PropertyOrder(-174)]
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public Image ImageEnter
        {
            get { return imageEnter; }
            set
            {
                if (this.imageEnter == value)
                    return;

                this.imageEnter = value;
                this.Invalidate();
            }
        }

        #region 临时存储

        /// <summary>
        /// 按钮自定义保存数据
        /// </summary>
        [DefaultValue(null)]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object Data { get; set; }

        /// <summary>
        /// 按钮rect（包含:外边距、内边距、内容）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected internal RectangleF M_Rect { get; set; }

        /// <summary>
        /// 按钮rect（只包含:内容）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        protected internal RectangleF C_Rect { get; set; }

        #endregion

        #endregion

        #region 字段

        /// <summary>
        /// 全局自定义按钮所属TabControl
        /// </summary>
        private TabControlPlus owner;

        #endregion

        public TabControlPlusGlobalCustomButtton()
        {

        }

        #region 虚方法

        internal protected virtual void OnGlobalCustomButttonClick(TabControlPlusGlobalCustomButttonEventArgs e)
        {
            if (this.globalCustomButttonClick != null && this.owner != null && this.owner is TabControlPlus)
            {
                this.globalCustomButttonClick(this.owner, e);
            }
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 控件重绘
        /// </summary>
        private void Invalidate()
        {
            if (this.owner != null)
            {
                TabControlPlus tabControl = (TabControlPlus)this.owner;
                tabControl.Invalidate();
            }
        }

        /// <summary>
        /// 控件重新初始化和重绘
        /// </summary>
        private void InitializeInvalidate()
        {
            if (this.owner != null)
            {
                TabControlPlus tabControl = (TabControlPlus)this.owner;
                tabControl.InitializeRectangle();
                tabControl.Invalidate();
            }
        }

        #endregion

        #region 公开方法

        /// <summary>
        /// 设置全局自定义按钮所属的控件
        /// </summary>
        /// <param name="owner">设置全局自定义按钮所属的控件</param>
        internal void SetOwner(TabControlPlus owner)
        {
            this.owner = owner;
        }

        /// <summary>
        /// 获取按钮Rectangle信息（只包含:内容）
        /// </summary>
        /// <returns></returns>
        public RectangleF GetRectangle()
        {
            return new RectangleF(this.C_Rect.X, this.C_Rect.Y, this.C_Rect.Width, this.C_Rect.Height);
        }

        #endregion

    }

}
